/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package Service;

import dao.OrderDao;
import dao.PaymentDao;
import common.JavaBean.Order;
import common.utils.PageInfo;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderService {
    private OrderDao orderDao = new OrderDao();
    private PaymentDao paymentDao = new PaymentDao();

    // 管理员功能
    public boolean adminCancelOrder(int orderId) {
        try {
            orderDao.adminCancelOrder(orderId);
            return true;
        } catch (SQLException e) {
            throw new RuntimeException("取消订单失败", e);
        }
    }

    public List<Order> getAllOrders() {
        try (ResultSet rs = orderDao.queryOrders(null, null, null, null, null, null)) {
            List<Order> list = new ArrayList<>();
            while (rs.next()) {
                Order o = new Order();
                o.setOrderId(rs.getInt("order_id"));
                o.setUserId(rs.getInt("user_id"));
                o.setProductId(rs.getInt("product_id"));
                o.setStatus(rs.getInt("status"));
                o.setQuantity(rs.getInt("quantity"));
                o.setSnapshotPrice(rs.getBigDecimal("snapshot_price"));
                o.setTotalPrice(rs.getBigDecimal("total_price"));
                o.setCreatedAt(rs.getTimestamp("created_at"));
                o.setPaidAt(rs.getTimestamp("completed_at"));
                list.add(o);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException("获取订单失败", e);
        }
    }

    // 用户功能
    public Order createOrder(int userId, int productId, int qty, String address) {
        try {
            ResultSet rs = orderDao.createOrder(userId, productId, qty, address);
            if (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("new_order_id"));
                order.setTotalPrice(rs.getBigDecimal("total_price"));
                return order;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("创建订单失败", e);
        }
    }

    public boolean payOrder(int userId, int orderId, String pin) {
        try {
            return "SUCCESS".equals(paymentDao.processPayment(userId, orderId, pin));
        } catch (SQLException e) {
            throw new RuntimeException("支付订单失败", e);
        }
    }

    public boolean cancelOrder(int orderId) {
        try {
            orderDao.adminCancelOrder(orderId);
            return true;
        } catch (SQLException e) {
            throw new RuntimeException("取消订单失败", e);
        }
    }

    public List<Order> getUserOrders(int userId) {
        try (ResultSet rs = orderDao.queryOrders(null, userId, null, null, null, null)) {
            List<Order> list = new ArrayList<>();
            while (rs.next()) {
                Order o = new Order();
                o.setOrderId(rs.getInt("order_id"));
                o.setUserId(rs.getInt("user_id"));
                o.setProductId(rs.getInt("product_id"));
                o.setStatus(rs.getInt("status"));
                o.setQuantity(rs.getInt("quantity"));
                o.setSnapshotPrice(rs.getBigDecimal("snapshot_price"));
                o.setTotalPrice(rs.getBigDecimal("total_price"));
                o.setCreatedAt(rs.getTimestamp("created_at"));
                o.setPaidAt(rs.getTimestamp("completed_at"));
                list.add(o);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException("获取用户订单失败", e);
        }
    }
}
